import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import Cookie from "@u/cookie";
Vue.use(Router);

var router = new Router({
  routes: [
    {
      path: "/login",
      name: "login",
      component: e => require(["@v/layout/login"], e),
      meta: {
        skip_auth: true
      }
    },
    {
      path: "/",
      name: "index",
      component: e => require(["@v/layout/index"], e),
      redirect: { name: "home" },
      children: [
        {
          path: "/home",
          name: "home",
          component: e => require(["@v/layout/home"], e)
        }
      ]
    }
  ],
  // 页面路由切换返回顶部
  scrollBehavior() {
    return { x: 0, y: 0 };
  }
});
/* eslint-disable */
router.beforeEach((to, from, next) => {
  // 登录拦截
  if (Cookie.get('token')) {
    store.dispatch('setToken', Cookie.get('token'))
  }
  if (
    to.matched.some(record => record.meta.skip_auth !== true) &&
    !store.state.token
  ) {
    next({
      path: "/login"
    });
  } else {
    next();
  }
});

export default router;
/* eslint-disable */